<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {		
		var trustRegionId = getURLParameter("trustRegionId");
		var tdTable = $('#trust-district-table')
		.dataTable(
				{
					"sDom" : 'Zrtp',
					"sPaginationType" : "bootstrap",
					"iDisplayLength" : 15,
					"bServerSide" : true,
					"bPaginate" : true,
					"sAjaxSource" : "trustDistrictTableAjax.action",
					"aoSearchCols": [
		 				null,
		 				null,
		 				null,
		 				{"sSearch": "Yes"}
		 			],
					"aoColumnDefs" : [ {
						"fnRender" : function(o, val) {
							if (o.aData[3] == "Yes") {
								return '<a href=editTrustDistrict.action?trustRegionId=' + trustRegionId + '&trustDistrictId=' + o.aData[4] + '>' + o.aData[0] + '</a>';													
							} else {
								return '<a href="#" onClick=toActiveTrustDistrict(' + o.aData[4] + ',' + trustRegionId + ')>' + o.aData[0] + '</a>';
							}
						},
						"aTargets" : [0]
					} ],
					"fnServerParams": function ( aoData ) {
				  		aoData.push( { "name": "trustRegionId", "value": trustRegionId});
					}
				});	
		
		$('#check-active-trust-district').click(function() {
			var other = $('#check-active-trust-district:checked').val();
			if (other != undefined) {
				tdTable.fnFilter("", 3, true, false);
			} else {
				tdTable.fnFilter("Yes", 3, true, false);
			}
		});	
		
		var tdTab = getURLParameterTrustRegion(document.location.href);						
		if (tdTab == true){
			$("#0").removeClass("active");
			$("li#details-tab").removeClass("active");							
			
			$("li#trust-district-tab").addClass("active");
			$("#1").addClass("active");
		}
	});

	function toActiveTrustDistrict(trustDistrictId, trustRegionId) {
		var r = confirm('Do you want to make this Trust District active?');
		if (r == true) {		
			window.location.replace("activeTrustDistrict.action?trustRegionId=" + trustRegionId + "&trustDistrictId=" + trustDistrictId);
		}
	};
	
	function getURLParameter(name) {
	    return decodeURI(
	        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
	    );
	}
	
	function getURLParameterTrustRegion(url) {
		var n=url.indexOf("tab=1");	
		if (n != -1) return true;
		return false;
	}
</script>
<div class="tab-pane" id="1">
	<div class="row-fluid">
		<div class="span12">
			<div class="">				
			</div>
			<div id="trust-district" class="box-content">
				<form action="#" class='form-horizontal' method="post">
					<div class="control-group">
						<div id="inactive-div">
							<a href="addTrustDistrict.action?trustRegionId=<s:property value='trustRegionId' />" class="btn btn-success">Create</a>
							<label class="checkbox"><input type="checkbox"
								name="check-inactive" id="check-active-trust-district"> Include
								In-active</label>
						</div>
						<table id="trust-district-table"
							class="table table-striped table-bordered">
							<thead>
								<tr>
									<th>District Name</th>
									<th>Description</th>
									<th>Region</th>
									<th>Is Active?</th>
								</tr>
							</thead>
							<tbody>
							</tbody>
						</table>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>